<?php
/**
 * Created by yuxianjun001@icloud.com.
 * User: wuchen
 * Date: 2019/4/12
 * Time: 11:47
 */

namespace App\Services\Advertising;


use App\model\AdvertisingModel\AdvModel;
use App\Services\BaseService;
use Illuminate\Support\Facades\DB;

/**
 * 广告服务层
 * Class AdvService
 * @package App\Services\Advertising
 */
class AdvService extends BaseService
{
    protected $model;

    public function __construct()
    {
        $this->model = new AdvModel();
    }

    /**
     * 新增广告
     * @param Request $request
     * @return mixed
     */
    public function add($request)
    {
        try {
            $adv = AdvModel::where(
                ['name' => $request->name, 'deleted_at' => null])
                ->first();

            if (!empty($adv)) {
                return $this->response('', '存在同名广告', 500);
            }
            $this->model->name    = $request->name;
            $this->model->url     = $request->url;
            $this->model->pic_url = $request->picUrl;
            $this->model->status  = 1; //0-默认不启用，1-启用

            $this->model->position = $request->position;//1,2,3,4-代表轮播图1234，5-用户推广，6-骑手推广，7-支付推广
            $this->model->remark   = $request->remark;

            $res = $this->model->save();

            if (!empty($res)) {
                return $this->response('', '新增广告成功');
            } else {
                return $this->response('', '新增广告失败', 500);
            }

        } catch (\Exception $e) {
            return $this->response('', '新增广告异常', 505);
        }


    }

    /**
     * 编辑广告
     * @param Request $request
     * @return mixed
     */
    public function edit($request)
    {
        try {
            $adv = PlatFormGoodsModel::where(
                ['id' => $request->id, 'deleted_at' => null])
                ->first();

            if (empty($adv)) {
                return $this->response('', '广告不存在', 500);
            }

            $adv2 = PlatFormGoodsModel::where(
                ['name' => $request->name, 'deleted_at' => null])
                ->first();

            if (empty($adv2)) {
                return $this->response('', '广告名字重复', 500);
            }

            $data = [
                'name'    => $request->name,
                'url'     => $request->url,
                'pic_url' => $request->piUrl,
                'remark'  => $request->remark,//备注
                'status'  => $request->status,//状态0-下架，1-上架
            ];

            $res = $this->model->where('id', $request->id)->update($data);
            if (!empty($res)) {
                return $this->response('', '广告修改成功');
            } else {
                return $this->response('', '广告修改失败', 500);
            }

        } catch (\Exception $e) {
            return $this->response('', '广告修改异常', 505);

        }


    }

    /**
     * 删除广告
     * @param Request $request
     * @return mixed
     */
    public function del($request)
    {
        try {
            $adv = AdvModel::where('id', $request->id)->first();
            if (empty($adv)) {
                return $this->response('', '该广告不存在', 404);
            }

            $res = $this->model->where('id', $request->id)->delete();

            if ($res) {
                return $this->response('', '广告删除成功');
            }

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '广告删除异常');
        }

    }

    /**
     * 广告列表
     * @param Request $request
     * @return mixed
     */
    public function list($request)
    {
        try {
            $dbResult = DB::table('advertising')->where('deleted_at', null);
            if (!empty($request->name)) {
                $dbResult = $dbResult->where('name', 'like', '%' . $request->name . '%');
            }

            if (!empty($request->position)) {
                $dbResult = $dbResult->where('position', $request->position);
            }

            $res = $this->model->buildPaginate($dbResult, $request);

            return $this->response($res, '查询成功');

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '查询失败', 500);
        }

    }

}